package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.RecommendConfig;
import com.burgeon.backend.SHOP.entity.RecommendSwitch;
import com.burgeon.backend.SHOP.service.RecommendService;
import com.burgeon.backend.SHOP.service.ex.UpDataExceptionApp;
import com.burgeon.backend.entity.TemplateSwitch;
import com.burgeon.backend.service.ex.LoadingException;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@CrossOrigin
@RestController
@RequestMapping("shop/recommend")
public class RecommendController extends BaseController {
    @Autowired
    private RecommendService recommendService;

    /**
     * 获取奖励信息和推荐配置信息
     * @return
     */
    @RequestMapping("getRecommendConfig")
    public ResponseResult<RecommendConfig> getRecommendConfig(HttpSession session){
        String token = session.getAttribute("token").toString();
//        String uid = session.getAttribute("app_openid").toString();
        if (null == session.getAttribute("app_openid")) {
            throw new UpDataExceptionApp("登录失效，请重新登录");
        }

        RecommendConfig data = recommendService.getRecommendConfigByToken(token,session);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 获取要展示的会优惠
     * @param token
     * @return
     */
    @RequestMapping("getBonuses")
    public ResponseResult<RecommendSwitch> findTemplateSwitchValue(String token){
        RecommendSwitch data = recommendService.findRecommendSwitch(token);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * x新用户注册成功推进人获得奖励
     * @param referrerId
     * @param
     * @return
     */
    @RequestMapping("award")
    public ResponseResult<Integer> award(String referrerId,HttpSession session){
        System.err.println("推荐新用户成功奖励");
        System.err.println("推荐人的referrerId==================="+referrerId);
        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();
        System.err.println("推荐新用户成功奖励："+uid);
        if (null == uid){
            throw new LoadingException("null");
        }
        Integer data = recommendService.addRecommend(uid, referrerId, token);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 获取推荐人小程序码
     * @param
     * @return
     */
    @RequestMapping("recommendCode")
    public ResponseResult<String> recommendCode(HttpSession session){
        if (null == session.getAttribute("app_openid")) {
            throw new UpDataExceptionApp("登录失效，请重新登录");
        }
        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();
        String data = recommendService.recommendCode(uid, token);
        return new ResponseResult<>(SUCCESS,data);
    }




}
